package com.demo.ruiji.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.demo.ruiji.common.CustomerException;
import com.demo.ruiji.dto.DishDto;
import com.demo.ruiji.mapper.DishMapper;
import com.demo.ruiji.pojo.Dish;
import com.demo.ruiji.pojo.DishFlavor;
import com.demo.ruiji.pojo.Setmeal;
import com.demo.ruiji.service.DishFlavorService;
import com.demo.ruiji.service.DishService;
import com.demo.ruiji.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements DishService {

    @Autowired
    private DishFlavorService dishFlavorService;

    @Autowired
    private  SetmealService setmealService;

    /**
     * 新增菜品，同时保存对应的口味
     * @param dishDto
     */
    @Transactional
    public void saveWithFlavor(DishDto dishDto) {
        //保存菜品的基本信息到dish表
        this.save(dishDto);

        //获取菜品id
        Long dishId = dishDto.getId();

        //菜品id和口味关联起来
        List<DishFlavor> flavors = dishDto.getFlavors();
        flavors =flavors.stream().map((item) -> {
            item.setDishId(dishId);
            return item;
        }).collect(Collectors.toList());

        //保存菜品口味基本信息到口味表中
        dishFlavorService.saveBatch(flavors);

    }

    /**
     * 根据id查询菜品信息和对应的口味信息
     * @param id
     * @return
     */
    @Override
    public DishDto getByIdWithFlavor(Long id) {
        //查询菜品信息
        Dish dish = this.getById(id);

        DishDto dishDto = new DishDto();
        BeanUtils.copyProperties(dish,dishDto);

        //查询菜品口味信息
        LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DishFlavor::getDishId,dish.getId());
        List<DishFlavor> flavors = dishFlavorService.list(queryWrapper);
        //封装数据
        dishDto.setFlavors(flavors);

        return dishDto;
    }

    /**
     * 更新菜品信息
     * @param dishDto
     */

    @Override
    @Transactional
    public void updateWithFlavor(DishDto dishDto) {
        //更新菜品信息
        this.updateById(dishDto);

        //先清理当前的口味信息表，执行删除操作
        LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DishFlavor::getDishId,dishDto.getId());
        dishFlavorService.remove(queryWrapper);

        //添加提交的口味数据信息
        List<DishFlavor> flavors = dishDto.getFlavors();
//        for (DishFlavor flavor : flavors) {
//            flavor.setDishId(dishDto.getId());
//        }

        flavors =flavors.stream().map((item) -> {
            item.setDishId(dishDto.getId());
            return item;
        }).collect(Collectors.toList());

        dishFlavorService.saveBatch(flavors);
    }

    /**
     * 通过id批量删除菜品
     * @param ids
     */

    @Override
    public void removeDish(List<Long> ids) {

//        // 删除菜品,将is_delete改为1
//        LambdaUpdateWrapper<Dish> updateWrapper1 = new LambdaUpdateWrapper<>();
//        updateWrapper1.set(Dish::getIsDeleted, 1).in(Dish::getId, ids);
//        this.update(updateWrapper1);
//
//        // 删除菜品对应的口味信息，将is_delete改为1
//        LambdaUpdateWrapper<DishFlavor> updateWrapper2 = new LambdaUpdateWrapper<>();
//        updateWrapper2.set(DishFlavor::getIsDeleted, 1).in(DishFlavor::getDishId, ids);
//        dishFlavorService.update(updateWrapper2);
        // 判断菜品是否关联套餐，如果关联，抛出业务异常
        LambdaQueryWrapper<Setmeal> setmealLambdaQueryWrapper = new LambdaQueryWrapper<>();
        // 查询当前菜品是否关联了套餐
        setmealLambdaQueryWrapper.in(Setmeal::getId,ids).eq(Setmeal::getStatus,1);
        // 添加条件,如果菜品状态为起售售状态，则不能删除
        setmealLambdaQueryWrapper.ne(Setmeal::getStatus, 0);//ne:不等于

        // 如果存在关联套餐，抛出业务异常
        int count = setmealService.count(setmealLambdaQueryWrapper);
        if (count > 0){
            //如果不能删除，抛出异常
            throw new CustomerException("菜品与套餐绑定，不能删除，请修改套餐信息后再来修改");
        }

        // 物理删除菜品记录
        this.removeByIds(ids);

        // 物理删除关联的口味记录
        LambdaQueryWrapper<DishFlavor> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(DishFlavor::getDishId, ids);
        dishFlavorService.remove(wrapper);

    }

    /**
     * 更改状态
     * @param status
     * @param ids
     */

    @Override
    public void updateStatus(Integer status, List<Long> ids) {
        LambdaUpdateWrapper<Dish> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.set(Dish::getStatus,status).in(Dish::getId,ids);
        this.update(updateWrapper);
    }

}
